package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Blank;
import com.example.demo.entity.Choice;
import com.example.demo.entity.Short1;
import com.example.demo.service.ChoiceService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Choice)表控制层
 *
 * @author hjl
 * @since 2020-11-30 22:43:02
 */
@CrossOrigin
@RestController
@RequestMapping("choice")
        @Api(value="选择题",tags={"选择题操作接口"})
public class ChoiceController {

    /**
     * 服务对象
     */
    @Autowired
    private ChoiceService choiceService;

    @GetMapping("selectOne")
    public Choice selectOne(Integer id) {
        return this.choiceService.queryById(id);
    }

    /**
     * 根据属性查询 （当参数为空时候）查询所有
     *
     * @param choice
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Choice>> query(@RequestBody(required = false) Choice choice) {
        return ResponseEntity.ok(choiceService.queryByExample(choice));
    }

    /**
     * 插入一条数据
     *
     * @param choice
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<List<Choice>> add(@RequestBody Choice choice) {
        if(choice.getId()!=null){
            choiceService.update(choice);
        }else{
            choiceService.insert(choice);
        }
        return ResponseEntity.ok(choiceService.queryByExample(new Choice()));
    }

    /**
     * 根据主键更新数据
     *
     * @param choice
     * @return
     */
    @PutMapping("update")
    public ResponseEntity<List<Choice>> update(@RequestBody Choice choice) {
        choiceService.update(choice);
        return ResponseEntity.ok(choiceService.queryByExample(new Choice()));
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public ResponseEntity<List<Choice>> delete(@PathVariable("id") Integer id) {
        choiceService.deleteById(id);
        return ResponseEntity.ok(choiceService.queryByExample(new Choice()));
    }

    @PostMapping("delAll")
    public ResponseEntity<List<Choice>> deleteAll(@RequestBody Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            choiceService.deleteById(id[i]);
        }
        return ResponseEntity.ok(choiceService.queryByExample(new Choice()));
    }

    @PostMapping("findChoiceByCourseid")
    public Result findChoiceByCourseid( Integer courseId) {
        Choice choice=new Choice();
        choice.setObj2(courseId+"");
        return Result.success(choiceService.queryByExample(choice));
    }
}